<?php

namespace Apipad\Controller;

use Think\Controller;

class CustomerInfoController extends CommonController
{
    //私人定制列表
    public function srdz_list()
    {
        $detail = I('post.');
        $user_srdz_customer = M("user_srdz_customer");
        $page = $detail['page'] ? $detail['page'] : 1;
        $limit = $detail['limit'] ? $detail['limit'] : 15;
        $keyword = $detail['keyword'];
        $start_time = $detail['start_time'];
        $end_time = $detail['end_time'];
        if (!empty($start_time) || !empty($end_time)) {
            if (empty($start_time)) {
                $start_time = date('Y-m-d H:i:s', time());
            }
            if (empty($end_time)) {
                $end_time = date('Y-m-d H:i:s', time());
            }
            $start_time = strtotime($start_time);
            $end_time = strtotime($end_time);
            $where['add_time'] = array('between', [$start_time, $end_time]);
        }
        $uid = UID;
        $where['isdel'] = 0;
        $where['uid'] = $uid;
        if ($keyword) {
            $where['customer_sn|customer_name|mobile'] = ['like', '%' . $keyword . '%'];
        }
        $count = $user_srdz_customer->where($where)->count();
        $list = $user_srdz_customer
            ->where($where)
            ->order('id desc')->page($page, $limit)->select();
        foreach ($list as $k => $v) {
            $list[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
        }
        $data_cc['code'] = 200;
        $data_cc['msg'] = '查询成功';
        $data_cc['data']['count'] = $count;
        $data_cc['data']['list'] = $list;
        exit(json_encode($data_cc));
    }

    //删除订单
    public function order_del()
    {
        $detail = I('post.');
        $uid = UID;
        $id = $detail["customer_id"];
        if (empty($id)) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = "参数错误";
            exit(json_encode($data_cc));
        }
        $user_srdz_customer = M("user_srdz_customer");
        $where['uid'] = $uid;
        $where['id'] = $id;
        $where['isdel'] = 0;
        $info = $model_user_order->where($where)->find();
        if ($info) {
            $this->cleart($id);
            $data_cc['code'] = 200;
            $data_cc['msg'] = "删除成功";
            exit(json_encode($data_cc));
        } else {
            $data_cc['code'] = 0;
            $data_cc['msg'] = "该顾客信息不存在";
            exit(json_encode($data_cc));
        }
    }

    function cleart($customer_id)
    {
        $uid = UID;
        $user_private_customer = M("user_srdz_customer");
        $user_private_order = M("user_srdz_order");
        $user_private_sybz = M("user_srdz_sybz");
        $user_private_tstx = M("user_srdz_tstx");
        $user_tstx_pic = M("user_srdz_pic");
        $user_private_customer->where(['id' => $customer_id, 'uid' => $uid, 'isdel' => 0])->save(['isdel' => 1]);
        $user_private_order->where(['customer_id' => $customer_id, 'uid' => $uid, 'isdel' => 0])->save(['isdel' => 1]);
        $user_private_sybz->where(['customer_id' => $customer_id, 'uid' => $uid, 'isdel' => 0])->save(['isdel' => 1]);
        $user_private_tstx->where(['customer_id' => $customer_id, 'uid' => $uid, 'isdel' => 0])->save(['isdel' => 1]);
        $user_tstx_pic->where(['customer_id' => $customer_id, 'uid' => $uid, 'isdel' => 0])->save(['isdel' => 1]);
    }

    //获取私人定制信息列表信息
    public function sget_srdz_info()
    {
        $detail = I('post.');
        $uid = UID;
        $customer_id = $detail['customer_id'] ?: 0;
        if (empty($customer_id)) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = "customer_id参数错误";
            exit(json_encode($data_cc));
        }
        $where['uid'] = $uid;
        $where['id'] = $customer_id;
        $model_user_srdz_customer = M("user_srdz_customer");
        $info = $model_user_srdz_customer->where($where)->find();
        if (empty($info)) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = "信息查询失败";
            exit(json_encode($data_cc));
        }
        $result['order_info'] = $info;
        $result['info'] = $this->customer_info($customer_id);
        $data_cc['code'] = 200;
        $data_cc['msg'] = '查询成功';
        $data_cc['data'] = $result;
        exit(json_encode($data_cc));
    }

    function customer_info($customer_id)
    {
        $uid = UID;
        $srdz_customer_model = M("user_srdz_customer");
        $srdz_order_model = M("user_srdz_order");
        $srdz_sybz_model = M("user_srdz_sybz");
        $srdz_tstx_model = M("user_srdz_tstx");
        $srdz_pic_model = M("user_srdz_pic");
        $info = $srdz_customer_model->where(['id' => $customer_id, 'uid' => $uid, 'isdel' => 0])->field('id,customer_name,sex,age,high,weight,cat_id,upper_id,below_id,mobile,customer_sn,remark')->find();
        $tuanti_list = $srdz_order_model->where(['customer_id' => $customer_id, 'uid' => $uid, 'isdel' => 0])->field('id,names,bodysize,pro_size,shiyi_fitting')->order('id asc')->select();
        $shiyibeizhu = $srdz_sybz_model->where(['customer_id' => $customer_id, 'uid' => $uid, 'isdel' => 0])->field('id,taomayi,palin,jianxie,houyaojie')->find();
        //$tesutix=$user_private_tstx->where(['order_id'=>$order_id,'uid'=>$uid,'isdel'=>0])->field('id,cid,pid,remark')->order('id asc')->select();
        $tesutix = $srdz_tstx_model->alias('a')->join('`fa_renshi_img` as b on a.cid=b.id')->where(['a.customer_id' => $customer_id, 'a.uid' => $uid, 'a.isdel' => 0])->field('a.id,a.cid as tx_id,a.pid,a.remark,b.title,b.img as pic,b.pid as cat_id')->order('id asc')->select();
        if ($tesutix) {
            /*
            foreach($tesutix as $k=>$val){
               $m_int=$this->get_childsc($val['cid']);
               $tesutix[$k]['tx_id']=$m_int['id'];
               $tesutix[$k]['title']=$m_int['title'];
               $tesutix[$k]['pic']=$m_int['pic'];
            }
            */
        }
        $picc = $srdz_pic_model->where(['customer_id' => $customer_id, 'uid' => $uid, 'isdel' => 0])->getField('picc');
        $mt['customer_info'] = $info;
        $mt['tuanti_list'] = $tuanti_list ?: [];
        $mt['shiyibeizhu'] = $shiyibeizhu ?: [];
        $mt['tesutix'] = $tesutix ?: [];
        $mt['kh_picc'] = $picc ?: "";
        return $mt;
    }
}